package edu.gdin.ecommerce.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import edu.gdin.ecommerce.model.Func;
import edu.gdin.ecommerce.model.Role;
import edu.gdin.ecommerce.service.IFuncService;
import edu.gdin.ecommerce.service.IRoleService;

@Component("roleAction")
@Scope("prototype")
public class RoleAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5636652933236867987L;
	
	private IRoleService roleService;
	private IFuncService funcService;
	private List<Role> roles;
	private List<Func> funcs;
	private Role role;
	
	private String roleName = "";
	private String selectedIds = "";
	private String msg = "";

	public IRoleService getRoleService() {
		return roleService;
	}
	@Resource
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	public IFuncService getFuncService() {
		return funcService;
	}
	@Resource
	public void setFuncService(IFuncService funcService) {
		this.funcService = funcService;
	}
	public List<Role> getRoles() {
		return roles;
	}
	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}
	public List<Func> getFuncs() {
		return funcs;
	}
	public void setFuncs(List<Func> funcs) {
		this.funcs = funcs;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getSelectedIds() {
		return selectedIds;
	}
	public void setSelectedIds(String selectedIds) {
		this.selectedIds = selectedIds;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	// ----------  methods  -----------
	
	
	public String getAllRoles() {
		this.roles = this.roleService.getAllRoles();
		return "getAllRoles";
	}
	
	
	public String queryRole() {
		this.roles = this.roleService.queryRole(this.roleName);
		if(this.roles.size() == 0) {
			this.addFieldError("msg", "没找到符合条件的角色！");
		}
		return "queryRole";
	}
	
	
	public String preAddRole() {
		return "preAddRole";
	}
	
	
	public String addRole() {
		if(this.roleService.isExist(this.role)) {
			this.addFieldError("error", "存在同名角色！");
			return "preAddRole";
		}
		
		this.roleService.addRole(this.role);
		return "addRole";
	}
	
	
	public String preEditRole() {
		this.role = this.roleService.getRoleById(this.role.getRoleId());
		return "preEditRole";
	}
	
	
	public String editRole() {
		this.roleService.editRole(this.role);
		return "editRole";
	}
	
	
	public String delRole() {
		this.roleService.delRole(this.role);
		return "delRole";
	}
	
	
	public String preSetFunc() {
		this.funcs = this.funcService.getAllFuncs();
		this.role = this.roleService.getRoleWithFuncById(this.role.getRoleId());
		return "preSetFunc";
	}
	
	
	public String setFunc() {
		this.roleService.setFunc(this.role, this.selectedIds);
		return "setFunc";
	}
	
}
